#include"LinpopHeaders.h"

const gchar *Register_UsernameEntry_char;
const gchar *Register_PasswdEntry_char;
const gchar *Register_PsdconfEntry_char;
const gchar *Register_GenderEntry_char;
const gchar *Register_AgeEntry_char;
const gchar *Register_DepartEntry_char;
const gchar *Register_NicknameEntry_char;
const gchar *Register_TelephoneEntry_char;
const gchar *Register_BirthEntry_char;

gchar Register_getUsername[1024] = {0};
gchar Register_getPasswd[1024] = {0};
gchar Register_getPsdconf[1024] = {0};

gchar Register_userinfo[1024] = {0};
gchar buf[1024] = {0};
struct sockaddr_in sin_addr_send,sin_addr_recv;
int socket_send,socket_recv;
int ret;
int sin_len_send,sin_len_recv;

pthread_t Register_Listenthread_t;

void *Register_Listenthread(void *arg)
{
        sin_addr_recv.sin_family = AF_INET;
        sin_addr_recv.sin_addr.s_addr = INADDR_ANY;
        sin_addr_recv.sin_port = htons(PORT);
        sin_len_recv = sizeof(sin_addr_recv);

        if((socket_recv = socket(AF_INET,SOCK_DGRAM,0)) == -1)                                                                     //接受注册成功/失败信息
        {
                perror("Socket");
                exit(1);
        }
        if (bind(socket_recv,(struct sockaddr *)&sin_addr_recv,sizeof(sin_addr_recv)) == -1)
        {
                perror("Bind");
                exit(1);
        }
        while(1)
        {
                if (recvfrom(socket_recv,buf,sizeof(buf),0,(struct sockaddr*)&sin_addr_recv,(socklen_t *)&sin_len_recv) != -1)
                {
                        g_print("%s\n",buf);
                        break;
                }
        }
        close(socket_recv);
        return (void *)0;
}

void on_Register_CancelButton_clicked(GtkObject *object,gpointer user_data)
{
        gtk_widget_hide(Register_Window);
        gtk_widget_show(Login_Window);


}

void on_Register_ConfirmButton_clicked(GtkObject *object, gpointer user_data)
{
        gchar *Gender_combox = NULL;
        gchar *Branch_combox = NULL;

        Register_UsernameEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_UsernameEntry));
        Register_PasswdEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_PasswdEntry));
        Register_PsdconfEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_PsdconfEntry));
        //Register_GenderEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_GenderEntry));
        Register_AgeEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_AgeEntry));
        //Register_DepartEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_DepartEntry));
        Register_NicknameEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_NicknameEntry));
        Register_TelephoneEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_TelephoneEntry));
        Register_BirthEntry_char = gtk_entry_get_text(GTK_ENTRY(Register_BirthEntry));


        Gender_combox = gtk_combo_box_get_active_text(GTK_COMBO_BOX(Register_GenderCombox));
        Branch_combox = gtk_combo_box_get_active_text(GTK_COMBO_BOX(Register_BranchCombox));

        strcpy(Register_getUsername,Register_UsernameEntry_char);
        strcpy(Register_getPasswd,Register_PasswdEntry_char);
        strcpy(Register_getPsdconf,Register_PsdconfEntry_char);

	if(strcmp(Register_PasswdEntry_char,Register_PsdconfEntry_char) != 0)
	{
		Psderror_builder = gtk_builder_new();
                gtk_builder_add_from_file(Psderror_builder,"Psderror.glade",NULL);

                Psderror_Window = GTK_WIDGET(gtk_builder_get_object(Psderror_builder,"Psderror_Window"));
                Psderror_Button = GTK_WIDGET(gtk_builder_get_object(Psderror_builder,"Psderror_Button"));

                g_signal_connect(G_OBJECT(Psderror_Button),"clicked",G_CALLBACK(on_Psderror_Button_clicked),NULL);
                gtk_builder_connect_signals(Psderror_builder,NULL);
                g_object_unref(G_OBJECT(Psderror_builder));
                gtk_widget_show(Psderror_Window);
                gtk_main();
	}
	else if((Register_getUsername[0] == '\0') || (Register_getPasswd[0] == '\0') || (Register_getPsdconf[0] == '\0'))
	{
                g_print("填写必要信息！\n");
                Necinfo_builder = gtk_builder_new();
                gtk_builder_add_from_file(Necinfo_builder,"Necinfo.glade",NULL);

                Necinfo_Window = GTK_WIDGET(gtk_builder_get_object(Necinfo_builder,"Necinfo_Window"));
                Necinfo_Button = GTK_WIDGET(gtk_builder_get_object(Necinfo_builder,"Necinfo_Button"));

                g_signal_connect(G_OBJECT(Necinfo_Button),"clicked",G_CALLBACK(on_Necinfo_Button_clicked),NULL);
                gtk_builder_connect_signals(Necinfo_builder,NULL);
                g_object_unref(G_OBJECT(Necinfo_builder));
                gtk_widget_show(Necinfo_Window);
                gtk_main();

        }

        else
        {
                bzero(buf, sizeof(buf));
                memset(Register_userinfo,0,1024);
                Register_userinfo[0] = '1';
                strcat(Register_userinfo,"~");                                                                                                                                //组包
                strcat(Register_userinfo,Register_UsernameEntry_char);
                strcat(Register_userinfo,"~");
                //destIP
                strcat(Register_userinfo,"~");
                strcat(Register_userinfo,Register_PasswdEntry_char);
                strcat(Register_userinfo,"~");
                //sourceIP
                strcat(Register_userinfo,"~");
                //destName
                strcat(Register_userinfo,"~");
                //nickname
                strcat(Register_userinfo,Register_NicknameEntry_char);
                strcat(Register_userinfo,"~");
                //state
                strcat(Register_userinfo,"~");
                strcat(Register_userinfo,Gender_combox);
                strcat(Register_userinfo,"~");
                strcat(Register_userinfo,Register_AgeEntry_char);
                strcat(Register_userinfo,"~");
                strcat(Register_userinfo,Branch_combox);
                strcat(Register_userinfo,"~");
                //tel
                strcat(Register_userinfo,Register_TelephoneEntry_char);
                strcat(Register_userinfo,"~");
                //birth
                strcat(Register_userinfo,Register_BirthEntry_char);
                strcat(Register_userinfo,"~");
                //profilepic
                strcat(Register_userinfo,"~");
                //msg
                strcat(Register_userinfo,"~");

                g_print("%s\n",Register_userinfo);

                bzero(&sin_addr_send,sizeof(sin_addr_send));
                bzero(&sin_addr_send,sizeof(sin_addr_send));
                sin_addr_send.sin_family = AF_INET;
                sin_addr_send.sin_addr.s_addr = inet_addr(SERVERIP);
                sin_addr_send.sin_port = htons(PORT);
                sin_len_send = sizeof(sin_addr_send);
                if((socket_send = socket(AF_INET,SOCK_DGRAM,0)) == -1)                                                                              //发送注册信息
                {
                        perror("Socket");
                        exit(1);
                }
                else if((ret = sendto(socket_send,Register_userinfo,1024,0,(struct sockaddr*)&sin_addr_send,sin_len_send)) == -1)
		{
			perror("Send to");
			exit(1);
                }
                else
                {
                        pthread_create(&Register_Listenthread_t, NULL, Register_Listenthread,NULL);
                        int i = 0;
                        for(i = 0; i < 200; i++)
                        {
                                g_print("xxx\n");
                                if(buf[0] == '\0')
                                {
                                        g_print("bieshuile\n");
                                        usleep(10000);
                                }
                                else
                                {
                                        break;
                                }
                        }
                        close(socket_recv);
                        pthread_cancel(Register_Listenthread_t);
                        if(buf[0] == '\0')
                        {
                                //弹出服务器故障窗口
                                Servererror_builder = gtk_builder_new();
                                gtk_builder_add_from_file(Servererror_builder,"Servererror.glade",NULL);
                                Servererror_Window = GTK_WIDGET(gtk_builder_get_object(Servererror_builder,"Servererror_Window"));
                                Servererror_Button = GTK_WIDGET(gtk_builder_get_object(Servererror_builder,"Servererror_Button"));
                                g_signal_connect(G_OBJECT(Servererror_Button),"clicked",G_CALLBACK(on_Servererror_Button_clicked),NULL);
                                gtk_builder_connect_signals(Servererror_builder,NULL);
                                g_object_unref(G_OBJECT(Servererror_builder));
                                gtk_widget_show(Servererror_Window);
                                gtk_main();

                                g_print("Server Error!\n");
                                close(socket_send);
                        }
                        else
                        {
                                if(strcmp(buf,"Register_success") == 0)
                                {
                                        //弹出注册成功窗口
                                        Registersuccess_builder = gtk_builder_new();
                                        gtk_builder_add_from_file(Registersuccess_builder,"Registersuccess.glade",NULL);
                                        Registersuccess_Window = GTK_WIDGET(gtk_builder_get_object(Registersuccess_builder,"Registersuccess_Window"));
                                        Registersuccess_Button = GTK_WIDGET(gtk_builder_get_object(Registersuccess_builder,"Registersuccess_Button"));
                                        g_signal_connect(G_OBJECT(Registersuccess_Button),"clicked",G_CALLBACK(on_Registersuccess_Button_clicked),NULL);
                                        gtk_builder_connect_signals(Registersuccess_builder,NULL);
                                        g_object_unref(G_OBJECT(Registersuccess_builder));
                                        gtk_widget_show(Registersuccess_Window);
                                        gtk_main();
                                }
                                else if(strcmp(buf ,"Register_error") ==0)
                                {
                                        //弹出注册失败窗口
                                        Registerfail_builder = gtk_builder_new();
                                        gtk_builder_add_from_file(Registerfail_builder,"Registerfail.glade",NULL);
                                        Registerfail_Window = GTK_WIDGET(gtk_builder_get_object(Registerfail_builder,"Registerfail_Window"));
                                        Registerfail_Button = GTK_WIDGET(gtk_builder_get_object(Registerfail_builder,"Registerfail_Button"));
                                        g_signal_connect(G_OBJECT(Registerfail_Button),"clicked",G_CALLBACK(on_Registerfail_Button_clicked),NULL);
                                        gtk_builder_connect_signals(Registerfail_builder,NULL);
                                        g_object_unref(G_OBJECT(Registerfail_builder));
                                        gtk_widget_show(Registerfail_Window);
                                        gtk_main();
                                }
                        }
                }

        }
}
